﻿using System.Data;
using System.Data.SqlClient;

SqlDataAdapter adapter = new SqlDataAdapter(
    new SqlCommand(
        "select * from product",
        new SqlConnection("server=.;uid=sa;pwd=Wgzwgz111420;database=unit21")
    )
);

// DataSet dataSet = new DataSet();
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);

List<Product> list = new List<Product>();

foreach (DataRow row in dataTable.Rows) {
    // list.Add(new Product(Convert.ToInt32(row["Id"]),Convert.ToString(row["Name"]),Convert.ToDecimal(row["Prices"])));
    
    Product product = new Product();
    product.id = Convert.ToInt32(row["id"]);
    product.name = row["name"].ToString();
    if (row["price"] is not DBNull) {
        product.price = Convert.ToDecimal(row["price"]);
    }
} 

foreach (var product in list) {
    Console.WriteLine($"id={product.id}, name={product.name}, price={product.price}");
}

// 在面向对象中,datatable的使用率不高
class Product{
    public int id { get; set; }
    public string name { get; set; }
    public decimal price { get; set; }
    
}

